home *** CD-ROM | disk | FTP | other *** search
- /*
- Commodore 64 Emulator v0.4 Earle F. Philhower III
- Copyright (C) 1993-4 (st916w9r@dunx1.ocs.drexel.edu)
-
- (Somewhat)
- High Speed 68K CPU simulation by George T. Talbot
- (ugtalbot@mcs.drexel.edu)
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- #ifndef __MWERKS__
-
- #include "Processor.h"
- #include "Registers68K.h"
- #include "Flags68K.h"
- #include "Instructions68K.h"
- #include "Stack68K.h"
- #include "Modes68K.h"
- #include "Memory68K.h"
- #include "Keyboard.h"
-
- #define ONE_SCAN_LINE 35 /* CPU Cycles */
- #define ONE_FRAME 350 /* Screen lines */
-
- extern word (* instructf[256]) ();
- extern byte cycletimef[256];
- extern byte *RAM, **memory;
-
- #define SETUP_CPU \
- tsp = sp; \
- tpc = pc; \
- asm { \
- MOVEM.L D3-D7/A2-A3,-(A7) \
- MOVE.W tpc,rPC \
- MOVE.W tsp,rSP \
- MOVE.B a,rA \
- MOVE.B x,rX \
- MOVE.B y,rY \
- MOVE.B flags,rFLAGS \
- MOVEA.L RAM,rRAM \
- MOVE.L rRAM,rSTACK \
- ADDA.L #256,rSTACK \
- MOVEA.L memory,rMEMORY \
- }
-
- #define RESTORE_CPU \
- asm { \
- MOVE.W rPC,tpc \
- MOVE.W rSP,tsp \
- MOVE.B rA,a \
- MOVE.B rX,x \
- MOVE.B rY,y \
- MOVE.B rFLAGS,flags \
- MOVEM.L (A7)+,D3-D7/A2-A3 \
- } \
- sp = tsp; \
- pc = tpc;
-
- /* Had to enclose SetUpMemoryMap() by the CPU initialization so that the CPU isn't
- * disturbed when SetUpMemoryMap() is called, and so that the CPU registers can be
- * used in SetUpMemoryMap(), if necessary.
- */
- void redoMemoryMap(void)
- {
- word tsp, tpc;
-
- RESTORE_CPU
- SetUpMemoryMap();
- SETUP_CPU
- }
-
- #pragma nooptimize(redoMemoryMap)
-
- /* This function is a translation of the processor loop in Processor.c. */
- void CPU()
- {
- word tsp, tpc;
- byte cur_cycles;
- byte oldMemoryMap;
- word cur_scanLines;
-
- oldMemoryMap = RAM[1];
- cur_scanLines = 0;
- cur_cycles = 0;
-
- /* Put the 6510 registers in 68K registers. (See Registers68K.h) */
- SETUP_CPU
-
- /* Outside loop is one screen frame */
- do {
- /* Inside loop is one screen line */
- do {
- asm {
- /* Fetch the current instruction */
- CLR.L D1
- MOVE.W rPC,D1
- LSL.L #2,D1
- MOVEA.L (rMEMORY,D1.L),A2
- MOVE.B (A2),D1
- ANDI.W #0x00FF,D1
-
- /* Advance PC past the instruction byte (additional PC
- * advances will be done in the instructions, themselves).
- */
- PC(#1)
-
- /* Count up the CPU cycles executed so far */
- MOVE.B cur_cycles,D0
- LEA cycletimef,A2
- ADD.B (A2,D1.W),D0
- MOVE.B D0,cur_cycles
-
- /* Execute the instruction */
- LSL.W #2,D1
- LEA instructf,A2
- MOVE.L (A2,D1.W),A2
- JSR (A2)
-
- /* Set up the keyboard scan codes */
- CLR.W D0
- MOVE.L #0x0000DC00,D1
- MOVE.B (rRAM,D1.L),D0
- LEA scanCode,A2
- MOVE.B (A2,D0.W),D0
- ADDQ.W #1,D1
- MOVE.B D0,(rRAM,D1.L)
-
- /* Check for memory map changes, and change the memory map, if so */
- MOVE.B 1(rRAM),D0
- CMP.B oldMemoryMap,D0
- BEQ @1
- MOVE.B D0,oldMemoryMap
- BSR redoMemoryMap
- @1
- }
- }
- while (cur_cycles < ONE_SCAN_LINE);
-
- cur_cycles -= ONE_SCAN_LINE;
-
- /* Update the current VIC scan register */
- asm {
- MOVE.L #0x0000D012,D1
- ADDQ.B #1,(rRAM,D1.L)
- BNE @2
- SUBQ.L #1,D1
- EORI.B #0x80,(rRAM,D1.L)
- @2 MOVE.L #0x0000D018,D1
- ORI.B #1,(rRAM,D1.L)
- }
-
- ++cur_scanLines;
- }
- while (cur_scanLines <= ONE_FRAME);
-
- /* Put the 6510 registers back into the memory variables */
- RESTORE_CPU
- }
-
- #pragma nooptimize(CPU)
-
-
- #endif
-